package com.example.xianji.fragment;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.activity.Main_BaiKeActivity;
import com.example.xianji.activity.Main_FuWu_GuanLiActivity;
import com.example.xianji.activity.Main_GuanYu_XianJiActivity;
import com.example.xianji.activity.Main_HelpActivity;
import com.example.xianji.activity.Main_LoginActivity;
import com.example.xianji.activity.Main_GeRen_ZhongXinActivity;
import com.example.xianji.activity.Main_My_CollectionActivity;
import com.example.xianji.activity.Main_My_DuiJieActivity;
import com.example.xianji.activity.Main_My_ServiceActivity;
import com.example.xianji.activity.Main_News_GuanLiActivity;
import com.example.xianji.activity.Main_Product_GuanLiActivity;
import com.example.xianji.activity.Main_Regesiter_OneActivity;
import com.example.xianji.activity.Main_SheZhiActivity;
import com.example.xianji.activity.Main_Shop_LiuYan_GuanLiActivity;
import com.example.xianji.activity.Main_Skill_GuanLiActivity;
import com.example.xianji.activity.Main_VIP_GuangLiActivity;
import com.example.xianji.activity.Main_XuQiu_GuanLiActivity;
import com.example.xianji.activity.Main_YiJian_FanKuiActivity;
import com.example.xianji.activity.Main_ZhanHao_GuanliActivity;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.BGARefresh.BGANormalRefreshViewHolder;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CircleImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的 后台
 *
 * @author me
 */
public class MyXianJiFragment extends Fragment implements OnClickListener, BGARefreshLayoutDelegate {
    private RelativeLayout relative_yijian, relative_help, relative_guanyu_xianji, my_xuqiu_guanli, my_fuwu_guanli, me_zhanghao_guanli, me_news_guanli, me_skill_guanli, me_shop_liuyan_guanli, me_product_guanli, me_luntan_guanli, me_relativelayout_vip_service, me_baike;
    private LinearLayout login_register, linear_xinxi_text, linear_my_duijie, linear_my_collection, linear_my_service;
    private Button denglu_button, zhuce_button;
    //    private SharedPreferences sp;
//    private Editor ed;
    private TextView text_name, jifen_number;
    private ImageView imageView_shezhi, image_renzheng;
    private CircleImageView my_photo;
    private BGARefreshLayout me_PullToRefreshLayout;
    private View view = null;
    /***
     * 防止Context  空指针
     */
    private Context mContext = null;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                me_PullToRefreshLayout.endRefreshing();
            }
        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.my_fragment, container, false);
        init();
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.relative_yijian) {
            startActivity(new Intent(mContext, Main_YiJian_FanKuiActivity.class));
        } else if (v.getId() == R.id.relative_help) {
            startActivity(new Intent(mContext, Main_HelpActivity.class));
        } else if (v.getId() == R.id.relative_guanyu_xianji) {
            startActivity(new Intent(mContext, Main_GuanYu_XianJiActivity.class));
        } else if (v.getId() == R.id.denglu_button) {
            Intent intent = new Intent(mContext, Main_LoginActivity.class);
            /**
             * 记录登录 来源
             */
            intent.putExtra("origin_login", 50);
            MyXianJiFragment.this.startActivityForResult(intent, Contant.dongtai_login_requestCode);

        } else if (v.getId() == R.id.zhuce_button) {
            Intent intent = new Intent(mContext, Main_Regesiter_OneActivity.class);
            intent.putExtra("from", 1);
            startActivity(intent);

        } else if (v.getId() == R.id.my_xuqiu_guanli) {
            //需求管理
            startActivity(new Intent(mContext, Main_XuQiu_GuanLiActivity.class));

        } else if (v.getId() == R.id.my_fuwu_guanli) {
            startActivity(new Intent(mContext, Main_FuWu_GuanLiActivity.class));
        } else if (v.getId() == R.id.me_zhanghao_guanli) {
            startActivity(new Intent(mContext, Main_ZhanHao_GuanliActivity.class));

        } else if (v.getId() == R.id.imageView_shezhi) {

            startActivity(new Intent(mContext, Main_SheZhiActivity.class));
        } else if (v.getId() == R.id.linear_my_duijie) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                Intent intent = new Intent(mContext, Main_My_DuiJieActivity.class);
                intent.putExtra("duijie", 2);
                startActivity(intent);
            } else {
                startActivity(new Intent(mContext, Main_LoginActivity.class));
            }
        } else if (v.getId() == R.id.linear_my_collection) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                startActivity(new Intent(mContext, Main_My_CollectionActivity.class));
            } else {
                startActivity(new Intent(mContext, Main_LoginActivity.class));
            }
        } else if (v.getId() == R.id.linear_my_service) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                startActivity(new Intent(mContext, Main_My_ServiceActivity.class));
            } else {
                startActivity(new Intent(mContext, Main_LoginActivity.class));
            }

        } else if (v.getId() == R.id.me_news_guanli) {

            startActivity(new Intent(mContext, Main_News_GuanLiActivity.class));
        } else if (v.getId() == R.id.my_photo) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                startActivity(new Intent(mContext, Main_GeRen_ZhongXinActivity.class));
            } else {
            }

        } else if (v.getId() == R.id.me_luntan_guanli) {
            //进入论坛
            /**
             * 跳转到浏览器
             */
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            // intent.putExtra("fa", 1);
            Uri content_url = Uri.parse("http://m.bbs.xianjichina.com/");
            // intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
            intent.setData(content_url);
            startActivity(intent);

        } else if (v.getId() == R.id.me_product_guanli) {
            //产品管理
            startActivity(new Intent(mContext, Main_Product_GuanLiActivity.class));

        } else if (v.getId() == R.id.me_skill_guanli) {
            //技术成果管理
            startActivity(new Intent(mContext, Main_Skill_GuanLiActivity.class));

        } else if (v.getId() == R.id.me_shop_liuyan_guanli) {
            //店铺留言
            startActivity(new Intent(mContext, Main_Shop_LiuYan_GuanLiActivity.class));
        } else if (v.getId() == R.id.me_relativelayout_vip_service) {
            //VIP管理
            startActivity(new Intent(mContext, Main_VIP_GuangLiActivity.class));
        } else if (v.getId() == R.id.me_baike) {
            //百科入口
            startActivity(new Intent(mContext, Main_BaiKeActivity.class));
        }
    }

    public void get_information() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));

        JSONObject parmas = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.info,
                parmas, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        JSONObject object = array.getJSONObject(0);
                        text_name.setText(object.getString("nickname"));
                        Log.i("sssssawwww", object.getString("avatar"));
                        ImageLoader.getInstance().displayImage(object.getString("avatar"), my_photo);
                        jifen_number.setText(object.getInt("useful_points") + "");

                        if (object.getInt("member") > 2) {
                            me_relativelayout_vip_service.setVisibility(View.VISIBLE);
                        } else {
                            me_relativelayout_vip_service.setVisibility(View.GONE);
                        }
                        if (object.getString("authen").equals("D")) {
                            image_renzheng.setImageResource(R.drawable.wei_renzheng);

                        } else if (object.getString("authen").equals("Y")) {
                            image_renzheng.setImageResource(R.drawable.renzheng);
                        } else if (object.getString("authen").equals("N")) {

                            image_renzheng.setImageResource(R.drawable.wei_tongguo);
                        }
                        if (object.getString("type").equals("FP") || object.getString("type").equals("AP")) {
                            me_news_guanli.setVisibility(View.VISIBLE);


                        } else {
                            me_news_guanli.setVisibility(View.VISIBLE);
                        }


                        PreferenceEngine.getInstance().putString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TYPE, object.getString("type"));
                        PreferenceEngine.getInstance().putInt(Constant.SP.NAME, Constant.SP.KEY_UID, object.getInt("id"));

                        PreferenceEngine.getInstance().putInt(Constant.SP.NAME, Constant.SP.KEY_EXPERT, object.getInt("expert"));
                    } else {
                        Toast.makeText(mContext, response.getString("msg"), Toast.LENGTH_SHORT).show();
                        PreferenceEngine.getInstance().putBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false);
                        visibleOrGone();
                    }
                    handler.sendEmptyMessage(1);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    /**
     * 初始化
     */
    public void init() {
        relative_yijian = (RelativeLayout) view.findViewById(R.id.relative_yijian);
        relative_help = (RelativeLayout) view.findViewById(R.id.relative_help);
        relative_guanyu_xianji = (RelativeLayout) view.findViewById(R.id.relative_guanyu_xianji);
        denglu_button = (Button) view.findViewById(R.id.denglu_button);
        zhuce_button = (Button) view.findViewById(R.id.zhuce_button);
        my_xuqiu_guanli = (RelativeLayout) view.findViewById(R.id.my_xuqiu_guanli);
        my_fuwu_guanli = (RelativeLayout) view.findViewById(R.id.my_fuwu_guanli);

        /*****************百科入口*************************/
        me_baike = (RelativeLayout) view.findViewById(R.id.me_baike);

        me_zhanghao_guanli = (RelativeLayout) view.findViewById(R.id.me_zhanghao_guanli);

        login_register = (LinearLayout) view.findViewById(R.id.login_register);
        linear_xinxi_text = (LinearLayout) view.findViewById(R.id.linear_xinxi_text);
        text_name = (TextView) view.findViewById(R.id.text_name);
        imageView_shezhi = (ImageView) view.findViewById(R.id.imageView_shezhi);
        my_photo = (CircleImageView) view.findViewById(R.id.my_photo);
        jifen_number = (TextView) view.findViewById(R.id.jifen_number);
        image_renzheng = (ImageView) view.findViewById(R.id.image_renzheng);
        linear_my_duijie = (LinearLayout) view.findViewById(R.id.linear_my_duijie);
        linear_my_collection = (LinearLayout) view.findViewById(R.id.linear_my_collection);
        linear_my_service = (LinearLayout) view.findViewById(R.id.linear_my_service);
        me_news_guanli = (RelativeLayout) view.findViewById(R.id.me_news_guanli);
        me_PullToRefreshLayout = (BGARefreshLayout) view.findViewById(R.id.me_PullToRefreshLayout);
        me_skill_guanli = (RelativeLayout) view.findViewById(R.id.me_skill_guanli);
        me_shop_liuyan_guanli = (RelativeLayout) view.findViewById(R.id.me_shop_liuyan_guanli);
        me_product_guanli = (RelativeLayout) view.findViewById(R.id.me_product_guanli);
        me_luntan_guanli = (RelativeLayout) view.findViewById(R.id.me_luntan_guanli);
        me_relativelayout_vip_service = (RelativeLayout) view.findViewById(R.id.me_relativelayout_vip_service);
        me_PullToRefreshLayout.setDelegate(this);

        me_PullToRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(
                getActivity(), true));
//        sp = mContext.getSharedPreferences("text", mContext.MODE_PRIVATE);
//        ed = sp.edit();
        visibleOrGone();
        my_photo.setOnClickListener(this);
        imageView_shezhi.setOnClickListener(this);
        relative_help.setOnClickListener(this);
        relative_yijian.setOnClickListener(this);
        relative_guanyu_xianji.setOnClickListener(this);
        denglu_button.setOnClickListener(this);
        zhuce_button.setOnClickListener(this);
        my_xuqiu_guanli.setOnClickListener(this);
        my_fuwu_guanli.setOnClickListener(this);

        me_zhanghao_guanli.setOnClickListener(this);

        linear_my_duijie.setOnClickListener(this);
        linear_my_collection.setOnClickListener(this);
        linear_my_service.setOnClickListener(this);
        me_news_guanli.setOnClickListener(this);
        me_luntan_guanli.setOnClickListener(this);
        me_product_guanli.setOnClickListener(this);
        me_shop_liuyan_guanli.setOnClickListener(this);
        me_skill_guanli.setOnClickListener(this);
        me_relativelayout_vip_service.setOnClickListener(this);


        me_baike.setOnClickListener(this);
    }
 
/* @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode==Contant.change_password_requestCode&&resultCode==Contant.change_password_success_resultCode){
			Main_ZhanHao_GuanliActivity.this.setResult(Contant.change_password_success_resultCode);
			Main_ZhanHao_GuanliActivity.this.finish();
			
			mContext.finish();
		}
		
		}*/


    @Override
    public void onResume() {
        super.onResume();
	
	/*
		if(MainActivity.mCustomProgressDialog!=null){
			
			MainActivity.mCustomProgressDialog.dismiss();
			MainActivity.mCustomProgressDialog=null;
		}*/

        if (Contant.login_resume == 1) {
            Log.i("加载加载加载", "resume:" + Contant.login_resume + "" + "登录：" +
                    PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false));
            visibleOrGone();
            Contant.login_resume = 0;
        }
        if (Contant.exit == 1) {
            my_photo.setImageResource(R.drawable.photo);
            login_register.setVisibility(View.VISIBLE);
            linear_xinxi_text.setVisibility(View.GONE);
            text_name.setVisibility(View.GONE);
            relative_yijian.setVisibility(View.VISIBLE);
            relative_help.setVisibility(View.VISIBLE);
            me_news_guanli.setVisibility(View.GONE);
            relative_guanyu_xianji.setVisibility(View.VISIBLE);
            my_xuqiu_guanli.setVisibility(View.GONE);
            my_fuwu_guanli.setVisibility(View.GONE);

            me_zhanghao_guanli.setVisibility(View.GONE);

            imageView_shezhi.setVisibility(View.GONE);
            if (me_PullToRefreshLayout != null) {

                me_PullToRefreshLayout.endRefreshing();
            }
            Contant.exit = 0;
        }
        if (Contant.change_password_login == 1) {
            visibleOrGone();
            Contant.change_password_login = 0;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 正常登录的返回
         */
        if (requestCode == Contant.dongtai_login_requestCode && resultCode == Contant.dongtai_login_success_back) {
            //Toast.makeText(mContext, "进入进入吧", 0).show();
            /**
             * 刷新一下数据
             */
            visibleOrGone();
        }
    }

    public void visibleOrGone() {
        if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
            login_register.setVisibility(View.GONE);
            linear_xinxi_text.setVisibility(View.VISIBLE);
            text_name.setVisibility(View.VISIBLE);
            relative_yijian.setVisibility(View.GONE);
            relative_help.setVisibility(View.GONE);
            relative_guanyu_xianji.setVisibility(View.GONE);

            my_xuqiu_guanli.setVisibility(View.VISIBLE);
            my_fuwu_guanli.setVisibility(View.VISIBLE);

            me_zhanghao_guanli.setVisibility(View.VISIBLE);

            imageView_shezhi.setVisibility(View.VISIBLE);
            me_skill_guanli.setVisibility(View.VISIBLE);
            me_shop_liuyan_guanli.setVisibility(View.VISIBLE);
            me_product_guanli.setVisibility(View.VISIBLE);
            me_luntan_guanli.setVisibility(View.VISIBLE);
            me_news_guanli.setVisibility(View.VISIBLE);
            me_relativelayout_vip_service.setVisibility(View.VISIBLE);

            XJApplication.cachThreadPool.execute(new Runnable() {

                @Override
                public void run() {
                    Log.i("进入进入进入进入", "进入进入进入进入");
                    get_information();
                }
            });
        } else {
            me_PullToRefreshLayout.endRefreshing();
            my_photo.setImageResource(R.drawable.photo);
            login_register.setVisibility(View.VISIBLE);
            linear_xinxi_text.setVisibility(View.GONE);
            text_name.setVisibility(View.GONE);
            relative_yijian.setVisibility(View.VISIBLE);
            relative_help.setVisibility(View.VISIBLE);
            me_news_guanli.setVisibility(View.GONE);
            relative_guanyu_xianji.setVisibility(View.VISIBLE);
            my_xuqiu_guanli.setVisibility(View.GONE);
            my_fuwu_guanli.setVisibility(View.GONE);

            me_zhanghao_guanli.setVisibility(View.GONE);

            imageView_shezhi.setVisibility(View.GONE);
            me_skill_guanli.setVisibility(View.GONE);
            me_shop_liuyan_guanli.setVisibility(View.GONE);
            me_product_guanli.setVisibility(View.GONE);
            me_luntan_guanli.setVisibility(View.GONE);
            me_relativelayout_vip_service.setVisibility(View.GONE);

        }

    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        visibleOrGone();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }


}
